package com.showtao.front.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.showtao.common.security.SecurityContext;
import com.showtao.common.security.domain.User;
import com.showtao.common.security.enumeration.EUserStatus;
import com.showtao.common.security.service.UserService;
import com.showtao.front.domain.AdminMenu;
import com.showtao.front.domain.ArticleStatus;
import com.showtao.front.service.ContentService;
import com.showtao.front.service.ProductService;
import com.showtao.front.service.VehicleService;

@RequiresUser 
@Controller
public class AdminController {
    
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private UserService userService;
    
    @RequiresPermissions (value={"dashboard_view"})  
    @RequestMapping(value = "/admin")
    public String toHomePage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "db");
        return "admin/home";
    }
    
    @RequiresPermissions (value={"vehicle_admin"})  
    @RequestMapping(value = "/admin/vehicle_brand")
    public String toVehicleBrandPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "vb");
        return "admin/vehicle/vehicle_brand";
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_line")
    public String toVehicleLinePage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "vl");
        model.addAttribute("vbMap", vehicleService.findAllVehicleBrandMap());
        return "admin/vehicle/vehicle_line";
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_model")
    public String toVehicleModelPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "vm");
        model.addAttribute("vlMap", vehicleService.findAllVehicleLineMap());
        return "admin/vehicle/vehicle_model";
    }
    
    @RequiresPermissions (value={"product_admin"})
    @RequestMapping(value = "/admin/product_category")
    public String toProductCategoryPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "pc");
        return "admin/product/product_category";
    }
    
    @RequiresPermissions (value={"product_admin"})
    @RequestMapping(value = "/admin/product")
    public String toProductPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "pd");
        model.addAttribute("categories", productService.findAllProductCategories());
        model.addAttribute("vmMap", vehicleService.findAllVehicleModelMap());
        return "admin/product/product";
    }
    
    @RequiresPermissions (value={"product_admin"})
    @RequestMapping(value = "/admin/tag")
    public String toTagPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "tg");
        return "admin/content/tag";
    }
    
    @RequiresPermissions (value={"product_admin"})
    @RequestMapping(value = "/admin/message")
    public String toMessagePage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "lm");
        return "admin/content/message";
    }
    
    @RequiresPermissions (value={"content_admin"})
    @RequestMapping(value = "/admin/catalog")
    public String toCatalogPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "ct");
        model.addAttribute("catalogs", contentService.findAllCatalogInFlat());
        return "admin/content/catalog";
    }
    
    @RequiresPermissions (value={"content_admin"})
    @RequestMapping(value = "/admin/article")
    public String toArticlePage(Model model) throws Exception {
        ArticleStatus[] statusList = new ArticleStatus[]{
            ArticleStatus.DRAF,
            ArticleStatus.PUBLISHED,
            ArticleStatus.CANCELLED
        };
        model.addAttribute("statusList", statusList);
        model.addAttribute("selectedSideItem", "at");
        model.addAttribute("catalogs", contentService.findAllCatalogInFlat());
        model.addAttribute("tags", contentService.findAllTags());
        return "admin/content/article";
    }
    
    @RequiresPermissions (value={"user_admin"})
    @RequestMapping(value = "/admin/user")
    public String toUserPage(Model model) throws Exception {
        EUserStatus[] statusList = new EUserStatus[]{
                EUserStatus.ACTIVE,
                EUserStatus.INACTIVE,
                EUserStatus.CANCELLED
            };
        model.addAttribute("statusList", statusList);
        model.addAttribute("selectedSideItem", "ur");
        model.addAttribute("roles", userService.findAllRoles());
        return "admin/user/user";
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/role")
    public String toRolePage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "rl");
        model.addAttribute("permissions", userService.findAllPermissions());
        return "admin/user/role";
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/permission")
    public String toPermissionPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "pm");
        return "admin/user/permission";
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/log")
    public String toLogPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "lg");
        return "admin/user/log";
    }
    
    @RequiresPermissions (value={"system_admin"})
    @RequestMapping(value = "/admin/menu")
    public String toMenuPage(Model model) throws Exception {
        model.addAttribute("selectedSideItem", "mu");
        model.addAttribute("permissions", userService.findAllPermissions());
        return "admin/user/menu";
    }
    
    @ModelAttribute("menus")
    public Map<String, List<AdminMenu>> getAvailableMenus() {
        return userService.getAvailableMenus();
    }
}
